package 统计算法.有序数组中的单一元素;

public class yxszzddyys {

    public static void main(String[] args) {
//        int[] arr = {3,3,7,7,10,11,11};
        int[] arr = {1,2,2,3,3};
        System.out.println(singleNonDuplicate(arr));
    }
    public static int singleNonDuplicate(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        int mid = (left + right) >> 1;
        while(left < right){
            System.out.println("当前num "+nums[mid]);
            System.out.println("当前mid "+mid);
            if(mid == 0 || nums[mid] != nums[mid - 1] && nums[mid] != nums[mid + 1]){
                return nums[mid];
            }

            if(nums[mid] == nums[mid - 1] ){
                System.out.println("等于前一个数");
                if(((mid+1) & 1) == 1){
                    System.out.println("前半部分为奇数");
                    right = mid;
                }else{
                    System.out.println("前半部分为偶数");
                    left = mid + 1;
                }
            }else{
                System.out.println("等于后一个数");
                if(((nums.length-mid-1) & 1) == 1){
                    System.out.println("前半部分为奇数");
                    right = mid;
                }else{
                    System.out.println("前半部分为偶数");
                    left = mid + 1;
                }
            }
            mid = (left + right) >> 1;
        }
        return nums[mid];
    }
}
